import * as THREE from "three"
import { Pass, FullScreenQuad } from "./Pass"
export default class ShaderPass extends Pass {
  textureID: any
  uniforms: { [uniform: string]: THREE.IUniform<any> }
  material: THREE.ShaderMaterial
  fsQuad: FullScreenQuad
  constructor(e, t?: string) {
    super()
    this.textureID = void 0 !== t ? t : "tDiffuse"
    e instanceof THREE.ShaderMaterial
      ? ((this.uniforms = e.uniforms), (this.material = e))
      : e &&
        ((this.uniforms = THREE.UniformsUtils.clone(e.uniforms)),
        (this.material = new THREE.ShaderMaterial({
          defines: Object.assign({}, e.defines),
          uniforms: this.uniforms,
          vertexShader: e.vertexShader,
          fragmentShader: e.fragmentShader
        })))
    this.fsQuad = new FullScreenQuad(this.material)
  }
  render(e: THREE.WebGLRenderer, t: THREE.RenderTarget, s) {
    this.uniforms[this.textureID] && (this.uniforms[this.textureID].value = s.texture)
    this.fsQuad.material = this.material
    this.renderToScreen
      ? (e.setRenderTarget(null), this.fsQuad.render(e))
      : (e.setRenderTarget(t), this.clear && e.clear(e.autoClearColor, e.autoClearDepth, e.autoClearStencil), this.fsQuad.render(e))
  }
}
